
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_ENTER = 13;
var _oLastMenuItem=null, _iMenuLast = -1;
var _oPopup = null;
var _oATPopup = null;
var _maxRows;



function PopupMenu()
{

this.AddMenuItem = _addMenuItem;
this.AddNotification = _addNotification;
this.AddMoreLink = _addMoreLink;
this.AddSeperator = _addSeperator;
this.Show = _show;
this.Hide = _hide;
this.IsOpen = _isOpen;
this.loadATMenu = _loadATMenu;
this.notifyATMenuClose = _closeATMenu;
this.clickedATMenuItem = _clickedATMenuItem;

function MenuItem(menuid, fnCallback)
{
    this.menuid = menuid;

    this.fnCallback = fnCallback;
}

var _sMenuItemsHtml = "";
var _oMenuItems = new Array();
var _iMenuID = 0;
var _iMenuSeperator = 0;

function _addNotification(sName, sIconUrl)
{
    _addMenuGeneric(sName, null, "popupNotificationItem", sIconUrl, false, true);
}

function _addMoreLink(sName, fnCallback, bDisabled, sIconUrl)
{
    var showLink = (!IsNull(bDisabled) && (bDisabled == true))?false:true;
    var sTdClass = (!showLink)?"popupMnuDisabledItem":"popupMoreLinkItem";
    _addMenuGeneric(sName, fnCallback, sTdClass, sIconUrl, !bDisabled, true);
}
function _addMenuItem(sName, fnCallback, bDisabled, sIconUrl)
{
    var showLink = (!IsNull(bDisabled) && (bDisabled == true))?false:true;
    var sTdClass = (!showLink)?"popupMnuDisabledItem":"popupMnuItem";
    _addMenuGeneric(sName, fnCallback, sTdClass, sIconUrl, !bDisabled, false);
}
function _addMenuGeneric(sName, fnCallback, sTdClass, sIconUrl, bShowLink, bannerColumn)
{

_sMenuItemsHtml += "<tr class=\"popupMnuRow\"><td class=\"popupMnuItem\"";
_sMenuItemsHtml += " name=\"menuItem\" menuID=\"" + _iMenuID + "\"";


if(typeof(fnCallback) == "string")
{
    _sMenuItemsHtml += " onclick=\"" + CrmEncodeDecode.CrmHtmlAttributeEncode(fnCallback) + "\"";
}

_sMenuItemsHtml += " id=\"mnu" + _iMenuID + "\" nowrap><div class=\"" + sTdClass + "\">";


if ((!bannerColumn) || (!IsNull(sIconUrl)))
{
if (!IsNull(sIconUrl))
{
_sMenuItemsHtml += "<img alt=\"\" class=\"popupMenuIcon\" src=\"" + CrmEncodeDecode.CrmHtmlAttributeEncode(sIconUrl) + "\"/>";
}
else
{
_sMenuItemsHtml += "<span class=\"popupMenuIcon\">&nbsp;&nbsp;&nbsp;</span>";
}

_sMenuItemsHtml += "<span class=\"popupMenuIconSpace\">&nbsp;</span>";
}
_sMenuItemsHtml += "<span class=\"popupText\">" + CrmEncodeDecode.CrmHtmlEncode(sName) + "</span>";
if(bShowLink) _sMenuItemsHtml += "<a tabindex=-1 class='atLink' id='atmnu" + _iMenuID + "' onclick='return false;' href='javascript:onclick();' target='_self' title=\"" + CrmEncodeDecode.CrmHtmlAttributeEncode(sName) + "\"></a>";
_sMenuItemsHtml += "</div></td></tr>";


var oMenuItem = new MenuItem(_iMenuID, fnCallback);
_iMenuID++;
_oMenuItems.push(oMenuItem);
}

function _addSeperator()
{

_sMenuItemsHtml += "<tr height=1></td><td name=\"menuItem\" menuID=\"" + _iMenuID + "\" id=\"mnu" + _iMenuID + "\" class=\"seperator\" nowrap></td></tr>";


var oMenuItem = new MenuItem(_iMenuID);
_iMenuID++;
_iMenuSeperator++;
_oMenuItems.push(oMenuItem);
}

function bodyInnerHtml(bAddCloseButton)
{
var s = "<table id=\"tblMenu\" class=\"popupMenu\" cellpadding=\"0\" cellspacing=\"0\">";
s += _sMenuItemsHtml;
if (bAddCloseButton)
{
s += "<tr><td><button onclick=\"window.close();\" id=\"butClose\" class=\"button\">";
s += CrmEncodeDecode.CrmHtmlEncode(LOCID_CLOSE_BUTTON_LABEL);
s += "</button></td></tr>";
}
s += "</table>";
return s;
}
function _show(iX, iY, iWidth, iHeight, oElement, bIsAT)
{
if (bIsAT)
{
var atObject = new Object();
atObject.menuHtc = this;
atObject.atMenu = null;
openStdDlg(prependOrgName("/_controls/popupmenu/atpopup.aspx"), atObject, "60px", "20px", true, false, null);
return;
}

if (IsNull(iX) || IsNull(iY))
{
return;
}


_hide();

if(IsNull(_oPopup))
{
_oPopup = openPopup();





var oXmlHttp;
oXmlHttp = CreateXmlHttp();
oXmlHttp.open("Get", prependOrgName("/_controls/popupmenu/popupmenu.css"), false);
oXmlHttp.send(null);

_oPopup.document.createStyleSheet(prependOrgName("/_controls/popupmenu/popupmenu.css"));
_oPopup.document.body.innerHTML = bodyInnerHtml(false);
_oPopup.document.body.style.cursor = "hand";

var oTable = _oPopup.document.body;


oTable.attachEvent("onkeydown", popupMenuKeyHandler);
oTable.attachEvent("onkeyup", popupMenuKeySwallower);



var oTDs = _oPopup.document.getElementsByTagName("TD");
var i = oTDs.length - 1;
while(i>=0)
{
if (oTDs[i].className.indexOf("Item") != -1)
{
if (oTDs[i].menuID)
{
var fnCallback = _oMenuItems[parseInt(oTDs[i].menuID, 10)].fnCallback;
if(typeof(fnCallback) == "function")
{
oTDs[i].attachEvent("onclick", fnCallback);
}
oTDs[i].attachEvent("onmouseover", popupMouseMoveHandler);
}
}
i--;
}
}
_oPopup.show(0, 0, 0, 0);
if (IsNull(iWidth))
{
iWidth = _oPopup.document.all.tblMenu.offsetWidth;
}
if (IsNull(iHeight))
{
iHeight = _oPopup.document.all.tblMenu.offsetHeight;
}

if ( LOCID_UI_DIR=="RTL") iX = iX - iWidth + oElement.offsetWidth ;
_oPopup.show(iX, iY, iWidth, iHeight, oElement);


_maxRows = _iMenuID;

if (!IsNull(_oPopup) && _oPopup.isOpen && ((iX != 0) || (iY != 0)))
{
popupInit();
}
}

function _hide()
{
if(!IsNull(_oPopup))
{
_oPopup.hide();
_oPopup = null;

_oLastMenuItem = null;
}
}

function _isOpen()
{
if (!IsNull(_oPopup))
{
return _oPopup.isOpen;
}
return false;
}

function _loadATMenu(oPopup, atMenu)
{
_oATPopup = oPopup;


oPopup.document.body.innerHTML = bodyInnerHtml(true);


oPopup.document.title = CrmEncodeDecode.CrmHtmlEncode(formatString(LOCID_POPUP_MENU_LABEL, ""));


oPopup.document._oMenu = oPopup.document.body.firstChild;

var iW = Math.max(oPopup.document._oMenu.offsetWidth+8, 400);
var iH = Math.min(oPopup.document._oMenu.offsetHeight+20, window.screen.availHeight);

oPopup.dialogHeight = iH.toString() + "px";
oPopup.dialogWidth = iW.toString() + "px";
iW = Math.max(oPopup.document._oMenu.offsetWidth+8, 200);
iH = Math.min(oPopup.document._oMenu.offsetHeight+20, window.screen.availHeight);
oPopup.dialogHeight = iH.toString() + "px";
oPopup.dialogWidth = iW.toString() + "px";
oPopup.dialogTop = "20px";
oPopup.dialogLeft = "20px";


oPopup.document.lastMenuItem = null;
}

function _closeATMenu()
{
_oATPopup = null;
}

function _clickedATMenuItem()
{

var o = _oATPopup.window.event.srcElement;
while (o.tagName != "TABLE" && o.tagName != "TD")
{
o = o.parentElement;
}
if(o.tagName == "TABLE")
{

return;
}
var iMenuId = o.menuID;
for(var i=0; i<_oMenuItems.length; i++)
{
if(_oMenuItems[i].menuid == iMenuId)
{
if(!IsNull(_oMenuItems[i].fnCallback))
{
_oMenuItems[i].fnCallback();
_oATPopup.close();
_oATPopup = null;
break;
}
}
}
}
}
function popupInit()
{
var popupDocument = _oPopup.document;
popupResetGlow(_oLastMenuItem);
popupGlow(popupDocument.all.mnu0);
popupDocument.all.mnu0.focus();
}

function popupMouseMoveHandler()
{

var o = _oPopup.document.parentWindow.event.srcElement;
while (o.tagName != "TABLE" && o.tagName != "TD")
{
o = o.parentElement;
}
if (o===_oLastMenuItem || o.name!="menuItem") return;

popupResetGlow(_oLastMenuItem);
popupGlow(o);
}

function popupResetGlow(o)
{
if ((!IsNull(o)) && (o.firstChild.className.indexOf("ItemSel") != -1))
{
o.firstChild.className = o.firstChild.className.replace("ItemSel", "Item");
}
}

function popupGlow(o)
{

if (o.firstChild.className.indexOf("ItemSel") == -1)
{
o.firstChild.className = o.firstChild.className.replace("Item", "ItemSel");
}
_iMenuLast = parseInt(o.menuID, 10);
_oLastMenuItem = o;
}

function popupMenuKeyHandler()
{
var popupDocument = _oPopup.document;
switch(popupDocument.parentWindow.event.keyCode)
{
case KEY_DOWN:
if(_iMenuLast == -1)
{
popupGlow(popupDocument.all.mnu0);
}
else
{
popupResetGlow(popupDocument.all["mnu"+_iMenuLast]);
_iMenuLast = (++_iMenuLast) % _maxRows;
popupGlow(popupDocument.all["mnu"+_iMenuLast]);
}
break;

case KEY_UP:
if(_iMenuLast == -1)
{
popupGlow(popupDocument.all.mnu0);
}
else
{
popupResetGlow(popupDocument.all["mnu"+_iMenuLast]);
_iMenuLast = (_iMenuLast==0 ? (_maxRows-1) : --_iMenuLast);
popupGlow(popupDocument.all["mnu"+_iMenuLast]);
}
break;
}
popupDocument.parentWindow.event.cancelBubble = true;
popupDocument.parentWindow.event.returnValue = false;
}


function popupMenuKeySwallower()
{
var popupDocument = _oPopup.document;
switch(popupDocument.parentWindow.event.keyCode)
{
case 32:
case KEY_ENTER:
_oLastMenuItem.click();
break;

case 27:
if(_oPopup.isOpen) _oPopup.hide();
break;
}
popupDocument.parentWindow.event.cancelBubble = true;
popupDocument.parentWindow.event.returnValue = false;
}
